<?php 
//如果有php运行环境，只需把该文件扩展名改成 .php，然后上传到web目录即可在真实服务器上测试
header("Access-Control-Allow-Origin: *");
?>

<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<title>康虎云报表系统测试</title>
<style type="text/css">
#output {font-size: 12px; background-color:#F0FFF0;}
</style>
</head>

<body>
  <div style="width: 100%;text-align:center;">
    <h2>康虎云报表系统（Ver 1.3.0）</h2>
    <h3>打印测试（模式2）</h3>
    <div style="line-height: 1.5;">
    	<div style="width: 70%; text-align: left;">
    	<b>一、首先按下列步骤设置：</b><br/>
    	1、运行打印服务器；<br/>
    	2、按“停止”按钮停止服务；<br/>
    	3、打开“设置”区；<br/>
    	4、在“常用参数-->服务模式”中，选择“模式2”；<br/>
    	5、按“启动”按钮启动服务。
    	</div>
			<div style="width: 70%; text-align: left;">
			<b>二、按本页的“打印”按钮开始打印。</b><br/>
			</div><br/>
      <input type="button" id="btnPrint" value="打印" /><br/><br/>
      
      <div style="width: 70%; text-align: left; font-size: 12px;">
      	由于JavaScript在不同域名下访问会出现由来已久的跨域问题，所以正式部署到服务器使用时，要解决跨域问题。<br/>
      	对于IE8以上版本浏览器，只需增加一个reponse头：Access-Control-Allow-Origin即可，而对于php、jsp、asp/aspx等动态语言而言，增加一个response头是非常简单的事，例如：<br/>
      	<b>在php：</b><br/><span style="color: red;">
&lt;?php <br/>
header("Access-Control-Allow-Origin: *");<br/>
?&gt;<br/>
      	</span>
      	<b>在jsp：</b><br/><span style="color: red;">
&lt;%  <br/>
response.setHeader("Access-Control-Allow-Origin", "*");  <br/>
%&gt;<br/>
				</span>
				<b>在asp.net中：</b><br/><span style="color: red;">
Response.AppendHeader("Access-Control-Allow-Origin", "*");
				</span>，<br/>其他语言里，大家请自行搜索“ajax跨域”。而对于IE8以下的浏览器，大家可以自行搜索“IE6+Ajax+跨域”寻找解决办法吧，也可以联系我们帮助。
    	</div>
    </div>
  </div>
  <div id="output"></div>
</body>

<!-- 引入模式2所需的javascript支持库 -->
<script type="text/javascript" src="cfprint_mode2.min.js" charset="UTF-8"></script>

<!-- 构造报表数据 -->
<script type="text/javascript">
var _reportData = '{"template":"waybill.fr3","ver":3, "Tables":[ {"Name":"Table1", "Cols":[{"type":"str","size":255,"name":"HAWB#","required":false},{"type":"int","size":0,"name":"NO","required":false},{"type":"float","size":0,"name":"报关公司面单号","required":false},{"type":"integer","size":0,"name":"公司内部单号","required":false},{"type":"str","size":255,"name":"发件人","required":false},{"type":"str","size":255,"name":"发件人地址","required":false},{"type":"str","size":255,"name":"发件人电话","required":false},{"type":"str","size":255,"name":"发货国家","required":false},{"type":"str","size":255,"name":"收件人","required":false},{"type":"str","size":255,"name":"收件人地址","required":false},{"type":"str","size":255,"name":"收件人电话","required":false},{"type":"str","size":255,"name":"收货人证件号码","required":false},{"type":"str","size":255,"name":"收货省份","required":false},{"type":"float","size":0,"name":"总计费重量","required":false},{"type":"int","size":0,"name":"总件数","required":false},{"type":"float","size":0,"name":"申报总价（CNY）","required":false},{"type":"float","size":0,"name":"申报总价（JPY）","required":false},{"type":"int","size":0,"name":"件数1","required":false},{"type":"str","size":255,"name":"品名1","required":false},{"type":"float","size":0,"name":"单价1（JPY）","required":false},{"type":"str","size":255,"name":"单位1","required":false},{"type":"float","size":0,"name":"申报总价1（CNY）","required":false},{"type":"float","size":0,"name":"申报总价1（JPY）","required":false},{"type":"int","size":0,"name":"件数2","required":false},{"type":"str","size":255,"name":"品名2","required":false},{"type":"float","size":0,"name":"单价2（JPY）","required":false},{"type":"str","size":255,"name":"单位2","required":false},{"type":"float","size":0,"name":"申报总价2（CNY）","required":false},{"type":"float","size":0,"name":"申报总价2（JPY）","required":false},{"type":"int","size":0,"name":"件数3","required":false},{"type":"str","size":255,"name":"品名3","required":false},{"type":"float","size":0,"name":"单价3（JPY）","required":false},{"type":"str","size":255,"name":"单位3","required":false},{"type":"float","size":0,"name":"申报总价3（CNY）","required":false},{"type":"float","size":0,"name":"申报总价3（JPY）","required":false},{"type":"int","size":0,"name":"件数4","required":false},{"type":"str","size":255,"name":"品名4","required":false},{"type":"float","size":0,"name":"单价4（JPY）","required":false},{"type":"str","size":255,"name":"单位4","required":false},{"type":"float","size":0,"name":"申报总价4（CNY）","required":false},{"type":"float","size":0,"name":"申报总价4（JPY）","required":false},{"type":"int","size":0,"name":"件数5","required":false},{"type":"str","size":255,"name":"品名5","required":false},{"type":"float","size":0,"name":"单价5（JPY）","required":false},{"type":"str","size":255,"name":"单位5","required":false},{"type":"float","size":0,"name":"申报总价5（CNY）","required":false},{"type":"float","size":0,"name":"申报总价5（JPY）","required":false},{"type":"str","size":255,"name":"参考号","required":false},{"type":"AutoInc","size":0,"name":"ID","required":false}],"Data":[{"公司内部单号":730293,"发货国家":"日本","单价1（JPY）":null,"申报总价2（JPY）":null,"单价4（JPY）":null,"申报总价2（CNY）":null,"申报总价5（JPY）":null,"报关公司面单号":200303900791,"申报总价5（CNY）":null,"收货人证件号码":null,"申报总价1（JPY）":null,"单价3（JPY）":null,"申报总价1（CNY）":null,"申报总价4（JPY）":null,"申报总价4（CNY）":null,"收件人电话":"182-1758-9999","收件人地址":"上海市闵行区虹梅南路1660弄蔷薇八村139号502室","HAWB#":"860014010055","发件人电话":"03-3684-9999","发件人地址":" 1-1-13,Kameido,Koto-ku,Tokyo","NO":3,"ID":3,"单价2（JPY）":null,"申报总价3（JPY）":null,"单价5（JPY）":null,"申报总价3（CNY）":null,"收货省份":null,"申报总价（JPY）":null,"申报总价（CNY）":null,"总计费重量":3.20,"收件人":"张三丰2","总件数":13,"品名5":null,"品名4":null,"品名3":null,"品名2":null,"品名1":"纸尿片","参考号":null,"发件人":"NAKAGAWA SUMIRE 2","单位5":null,"单位4":null,"单位3":null,"单位2":null,"单位1":null,"件数5":null,"件数4":null,"件数3":3,"件数2":null,"件数1":10},{"公司内部单号":730291,"发货国家":"日本","单价1（JPY）":null,"申报总价2（JPY）":null,"单价4（JPY）":null,"申报总价2（CNY）":null,"申报总价5（JPY）":null,"报关公司面单号":200303900789,"申报总价5（CNY）":null,"收货人证件号码":null,"申报总价1（JPY）":null,"单价3（JPY）":null,"申报总价1（CNY）":null,"申报总价4（JPY）":null,"申报总价4（CNY）":null,"收件人电话":"182-1758-9999","收件人地址":"上海市闵行区虹梅南路1660弄蔷薇八村139号502室","HAWB#":"860014010035","发件人电话":"03-3684-9999","发件人地址":" 1-1-13,Kameido,Koto-ku,Tokyo","NO":1,"ID":1,"单价2（JPY）":null,"申报总价3（JPY）":null,"单价5（JPY）":null,"申报总价3（CNY）":null,"收货省份":null,"申报总价（JPY）":null,"申报总价（CNY）":null,"总计费重量":3.20,"收件人":"张三丰","总件数":13,"品名5":null,"品名4":null,"品名3":null,"品名2":null,"品名1":"纸尿片","参考号":null,"发件人":"NAKAGAWA SUMIRE","单位5":null,"单位4":null,"单位3":null,"单位2":null,"单位1":null,"件数5":null,"件数4":null,"件数3":3,"件数2":null,"件数1":10},{"公司内部单号":730292,"发货国家":"日本","单价1（JPY）":null,"申报总价2（JPY）":null,"单价4（JPY）":null,"申报总价2（CNY）":null,"申报总价5（JPY）":null,"报关公司面单号":200303900790,"申报总价5（CNY）":null,"收货人证件号码":null,"申报总价1（JPY）":null,"单价3（JPY）":null,"申报总价1（CNY）":null,"申报总价4（JPY）":null,"申报总价4（CNY）":null,"收件人电话":"182-1758-9999","收件人地址":"上海市闵行区虹梅南路1660弄蔷薇八村139号502室","HAWB#":"860014010045","发件人电话":"03-3684-9999","发件人地址":" 1-1-13,Kameido,Koto-ku,Tokyo","NO":2,"ID":2,"单价2（JPY）":null,"申报总价3（JPY）":null,"单价5（JPY）":null,"申报总价3（CNY）":null,"收货省份":null,"申报总价（JPY）":null,"申报总价（CNY）":null,"总计费重量":3.20,"收件人":"张无忌","总件数":13,"品名5":null,"品名4":null,"品名3":null,"品名2":null,"品名1":"纸尿片","参考号":null,"发件人":"NAKAGAWA SUMIRE 1","单位5":null,"单位4":null,"单位3":null,"单位2":null,"单位1":null,"件数5":null,"件数4":null,"件数3":3,"件数2":null,"件数1":10}]}]}';
if(window.console) console.log("reportData = " + _reportData);
</script>

<!-- 设置服务器参数 -->
<script language="javascript" type="text/javascript">
var cfprint_addr = "127.0.0.1";  //打印服务器监听地址
var cfprint_port = 54321;        //打印服务器监听端口
var _url = "http://"+cfprint_addr+":"+cfprint_port;
</script>

<!-- 编写回调函数用以处理服务器返回的数据 -->
<script type="text/javascript">
/**
 * 参数：
 * readyState: XMLHttpRequest的状态
 * httpStatus: 服务端返回的http状态
 * responseText: 服务端返回的内容
 */
var callbackSuccess = function(readyState, httpStatus, responseText){
	if (httpStatus === 200) {
  	//{"result": 1, "message": "打印完成"}
    var response = CFPrint.parseJSON(responseText);
    alert(response.message+", 状态码["+response.result+"]");
  }else{
  	alert('打印失败，HTTP状态代码是：'+httpStatus);
  }
}

/**
 * 参数：
 * message: 错误信息
 */
var callbackFailed = function(message){ 
	alert('发送打印任务出错: ' + message);
}	
</script>

<!-- 调用发送打印请求功能  -->
<script type="text/javascript">
(function(){
document.getElementById("btnPrint").onclick = function() {
	CFPrint.outputid = "output";  //指定调试信息输出div的id
	CFPrint.SendRequest(_url, _reportData, callbackSuccess, callbackFailed);  //发送打印请求
};
})();
</script>
</html>